package tr.edu.metu.is.mega.client.course.instructor;

import java.util.List;

import tr.edu.metu.is.mega.client.core.event.SelectionEventBus;
import tr.edu.metu.is.mega.client.core.event.SelectionEventListener;
import tr.edu.metu.is.mega.client.course.CourseChangeEvent;
import tr.edu.metu.is.mega.client.resource.MEGAResourceBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.service.GwtCourseService;
import tr.edu.metu.is.mega.client.service.GwtCourseServiceAsync;
import tr.edu.metu.is.mega.client.widgets.MegaTextField;
import tr.edu.metu.is.mega.common.dto.CourseDTO;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Container class for basic information of a course.
 */
public class CourseInformationPanel extends LayoutContainer {

    MEGAResourceBundle resourceBundle = MEGAResources.RESOURCEBUNDLE;

    private MegaTextField codeField;

    private MegaTextField instructorField;

    private MegaTextField nameField;

    private MegaTextField semesterField;

    private MegaTextField assistantsField;

    /**
     * Default constructor.
     */
    public CourseInformationPanel() {
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setLayout(new ColumnLayout());

        add(getLeft(), new ColumnData(0.33));

        add(getRight(), new ColumnData(0.66));

        setSearchAction();

        SelectionEventBus.getInstance().registerListener(CourseChangeEvent.Update.class, new SelectionEventListener<CourseDTO>() {

            @Override
            public void selectionChanged(CourseDTO selectedObject) {
                instructorField.setValue(selectedObject.getInstructor().getFullName());
                nameField.setValue(selectedObject.getName());
                semesterField.setValue(selectedObject.getSemester());
                assistantsField.setValue(selectedObject.getAssistantList());
                codeField.setValue(selectedObject.getCode());
            }
        });

    }

    /**
     * Returns left column of container.
     * 
     * @return layout container
     */
    public LayoutContainer getLeft() {
        LayoutContainer left = new LayoutContainer(new FormLayout());

        codeField = new MegaTextField(CourseDTO.FIELD_CODE);
        codeField.setFieldLabel(resourceBundle.courseCode());
//        codeField.setValue("IS501");
        codeField.focus();
        ToolTipConfig config = new ToolTipConfig();
        config.setTitle("Searching");
        config.setText("Enter course code to search..");
        config.setMouseOffset(new int[] { 0, 0 });
        config.setAnchor("left");
        codeField.setToolTip(config);

        left.add(codeField, new FormData("96%"));

        instructorField = new MegaTextField(CourseDTO.FIELD_INSTRUCTOR);
        instructorField.setFieldLabel(resourceBundle.courseInstructor());
        instructorField.setReadOnly(true);

        left.add(instructorField, new FormData("96%"));
        return left;
    }

    /**
     * Returns right column of container.
     * 
     * @return layout container
     */
    public LayoutContainer getRight() {
        nameField = new MegaTextField(CourseDTO.FIELD_NAME);
        nameField.setFieldLabel(resourceBundle.courseName());
        nameField.setReadOnly(true);

        semesterField = new MegaTextField(CourseDTO.FIELD_SEMESTER);
        semesterField.setFieldLabel(resourceBundle.courseSemester());
        semesterField.setReadOnly(true);

        assistantsField = new MegaTextField(null);
        assistantsField.setFieldLabel(resourceBundle.courseAssistants());
        assistantsField.setReadOnly(true);

        LayoutContainer right = new LayoutContainer(new RowLayout());

        LayoutContainer rightTopLeft = new LayoutContainer(new FormLayout());
        rightTopLeft.add(nameField, new FormData("96%"));

        LayoutContainer rightTopRight = new LayoutContainer(new FormLayout());
        rightTopRight.add(semesterField, new FormData("96%"));

        LayoutContainer rightTop = new LayoutContainer(new ColumnLayout());
        rightTop.add(rightTopLeft, new ColumnData(0.49));
        rightTop.add(rightTopRight, new ColumnData(0.49));

        LayoutContainer rightBottom = new LayoutContainer(new FormLayout());
        rightBottom.add(assistantsField, new FormData("96%"));

        right.add(rightTop, new RowData(1, -1));
        right.add(rightBottom, new RowData(1, -1));
        return right;
    }

    /**
     * Adds key listener to code field for searching.
     */
    public void setSearchAction() {
        codeField.addKeyListener(new KeyListener() {

            @Override
            public void componentKeyPress(ComponentEvent event) {
                super.componentKeyPress(event);
                if (event.getKeyCode() == KeyCodes.KEY_ENTER) {
                    CourseDTO dto = new CourseDTO();
                    dto.setCode(codeField.getValue());
                    getService().getCourses(dto, new AsyncCallback<List<CourseDTO>>() {

                        @Override
                        public void onSuccess(List<CourseDTO> result) {
                            if (result.size() == 1) {
                                SelectionEventBus.getInstance().fireEvent(CourseChangeEvent.UPDATE, result.get(0));
                            }
                        }

                        @Override
                        public void onFailure(Throwable caught) {
                        }
                    });

                }
            }
        });
    }

    /**
     * Returns GWT course service instance.
     * 
     * @return GWT service
     */
    public static GwtCourseServiceAsync getService() {
        return GwtCourseService.Util.getInstance();
    }

}
